﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class UIAP06 : ViewBase
    {
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (!IsManage)
            {
                VICC_WFEVENTS.SVWF.AP02(this.RUNID);
            }

            base.ManageCommand(sender, e);
        } 
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DARE01(dwMaster);
            filter = "APAEAEVOR='" + dwMaster["APAEAEVOR"].ToString() + "' AND APAEMODNR='" + dwMaster["APAEMODNR"].ToString() + "' AND APAEEINSTNR='" + dwMaster["APAEEINSTNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            //base.DeleteCommand(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";

            }
            else
            {
                KEY = TMV.APAEANRKRS + TMV.APAEALFDNR + TMV.APAEAFOLGE + TMV.APAEAEVOR + TMV.APAEMODNR + TMV.APAEEINSTNR ;
            }
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            VU1 = "";
            foreach (DataRow row in TBMain.Rows)
            {
                row["APAEWERTN"] = Helpers.Helper.Right("0000000000000000" + row["APAEWERTN"].ToString(), 16);
                row["APAEWERTX"] = (double.Parse(row["APAEWERTN"].ToString()) * 0.000001).ToString().PadRight(16);
                switch (row["AMEMESCHL"].ToString().Trim())
                {
                    case "1":
                        VU1 = (1000 * decimal.Parse(row["APAEWERTN"].ToString()) / 1000 / 1000).ToString();
                        break;
                }
                TMV.UpdateAPARTE(row, 0);
            }
            base.OnSave();
        }
    }
    public partial class UIAP06
    {
        public VAPARTE TMV
        {
            get
            {
                return (VAPARTE)MV;
            }
        }
        #region ==STDM===========
        public static readonly DependencyProperty STDMProperty = DependencyProperty.Register("STDM",
            typeof(string), typeof(UIAP06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSTDMChanged));
        public string STDM
        {
            get
            {
                return (string)GetValue(STDMProperty);
            }
            set
            {
                SetValue(STDMProperty, value);
                OnSTDMChg(value);
            }
        }
        private static void OnSTDMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAP06;
            if (src != null)
            {
                src.STDM = (string)e.NewValue;
            }
        }
        public virtual void OnSTDMChg(string value)
        { }
        #endregion

        #region ==MACHINEID===========
        public static readonly DependencyProperty MACHINEIDProperty = DependencyProperty.Register("MACHINEID",
            typeof(string), typeof(UIAP06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMACHINEIDChanged));
        public string MACHINEID
        {
            get
            {
                return (string)GetValue(MACHINEIDProperty);
            }
            set
            {
                SetValue(MACHINEIDProperty, value);
                OnMACHINEIDChg(value);
            }
        }
        private static void OnMACHINEIDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAP06;
            if (src != null)
            {
                src.MACHINEID = (string)e.NewValue;
            }
        }
        public virtual void OnMACHINEIDChg(string value)
        { }
        #endregion

        #region ==VU1===========
        public static readonly DependencyProperty VU1Property = DependencyProperty.Register("VU1",
            typeof(string), typeof(UIAP06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVU1Changed));
        public string VU1
        {
            get
            {
                return (string)GetValue(VU1Property);
            }
            set
            {
                SetValue(VU1Property, value);
                OnVU1Chg(value);
            }
        }
        private static void OnVU1Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIAP06;
            if (src != null)
            {
                src.VU1 = (string)e.NewValue;
            }
        }
        public virtual void OnVU1Chg(string value)
        { }
        #endregion
    }
}
